import axios from 'axios'
const HOST = 'http://127.0.0.1:3000/'

let config = {
  headers: {
    'Accept': 'application/json',
    'token': sessionStorage.getItem('token')
  }
}
export function get (url, params) {
  config['params'] = params
  return new Promise((resolve, reject) => {
    axios.get(HOST + url, config).then(response => {
      resolve(response.data)
    }).catch(error => {
      if (error.response) {
        // 请求已发出，但服务器响应的状态码不在 2xx 范围内
        console.log(error.response.data)
        console.log(error.response.status)
        console.log(error.response.headers)
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log('Error', error.message)
      }
      console.log(error.config)
    })
  })
}

export function post (url, params) {
  config.headers['token'] = sessionStorage.getItem('token')
  return new Promise((resolve, reject) => {
    axios.post(HOST + url, params, config).then(json => {
      resolve(json.data)
    })
  })
}
